﻿using Bardent.Weapons.Components.ComponentData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;

namespace Bardent.Weapons
{
    /**
     * 自定义脚本编辑器
     * */
    [CustomEditor(typeof(WeaponDataSO))]
    class WeaponDataSOEditor:Editor
    {

        private static List<Type> dataCompTypes = new List<Type>();
        //{
        //    typeof(MovementData),
        //    typeof(WeaponSpriteData)
        //};

        private WeaponDataSO dataSO;

        private bool showForceUpdateButtons;

        private bool showAddComponentButtons;

        private void OnEnable()
        {
            //定义dataSO 为武器数据
            dataSO = target as WeaponDataSO; 
        }

        public override void OnInspectorGUI()
        {

            base.OnInspectorGUI();

            if (GUILayout.Button("设置攻击次数"))
            {
                foreach (var item in dataSO.ComponentData)
                {
                    item.InitializeAttackData(dataSO.NumberOfAttacks);
                }
            }

            //折叠面板
            showAddComponentButtons = EditorGUILayout.Foldout(showAddComponentButtons, "添加组件");

            if (showAddComponentButtons)
            {
                foreach (var item in dataCompTypes)
                {
                    if (GUILayout.Button(item.Name))
                    {
                        //激活器类，创建一个类型对象
                        var comp = Activator.CreateInstance(item) as ComponentData;
                        if (comp == null)
                        {
                            return;
                        }

                        comp.InitializeAttackData(dataSO.NumberOfAttacks);

                        dataSO.AddData(comp);
                    }
                }
            }

            showForceUpdateButtons = EditorGUILayout.Foldout(showForceUpdateButtons, "更新名称");

            if (showForceUpdateButtons)
            {
                if (GUILayout.Button("更新组件数据名称"))
                {
                    foreach (var item in dataSO.ComponentData)
                    {
                        item.SetComponentName();
                    }
                }
                if (GUILayout.Button("更新攻击数据名称"))
                {
                    foreach (var item in dataSO.ComponentData)
                    {
                        item.SetAttackDataNames();
                    }
                }
            }

        }

        //可以在每次编译的时候调用，必须为静态函数
        [DidReloadScripts]
        private static void OnRecompile()
        {
            //Debug.Log("编译");
            //获取当前加载的所有程序集，例如类，接口，结构等一系列
            var assemblies = AppDomain.CurrentDomain.GetAssemblies();

            //获取到当前程序里所有类型
            var types = assemblies.SelectMany(assembly => assembly.GetTypes());
            //遍历数组，找出此类型
            var filteredTypes = types.Where(
                //查找此类型，并且并非通用类型,并且是class
                type => type.IsSubclassOf(typeof(ComponentData))&&!type.ContainsGenericParameters&&type.IsClass
                );
            //填充到列表
            dataCompTypes = filteredTypes.ToList();
        }
    }
}
